package com.lt.lumentracks.controller;


import com.lt.lumentracks.entity.po.ConversationUser;
import com.lt.lumentracks.entity.vo.common.Result;
import com.lt.lumentracks.service.IConversationUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 会话成员表 (关联表) 前端控制器
 * </p>
 *
 * @author swl
 * @since 2025-10-06
 */
@RestController
@RequestMapping("/conversation-user")
public class ConversationUserController {

    @Autowired
    private IConversationUserService iConversationUserService;

    // 增（用户加入会话）
    @PostMapping("/add")
    public Result<String> add(@RequestParam Long convId, @RequestParam Integer userId){
        ConversationUser cu = new ConversationUser();
        cu.setConversationId(convId);
        cu.setUserId(userId);
        cu.setCreateTime(LocalDateTime.now());
        iConversationUserService.save(cu);

        return Result.success();
    }


    // 改（修改用户在某会话中最后阅读的消息id）
    @PutMapping("/updateLastMsg")
    public Result<String> updateLastMsg(@RequestParam Long convId, @RequestParam Long lastMsgId_new){
        iConversationUserService.updateLastMsg(convId, lastMsgId_new);
        return Result.success();
    }

    // 改（修改用户在某会话中最后阅读的消息id）-- 只存入redis中，后使用定时任务批量更新数据库
    @PutMapping("/updateLastMsg/V2")
    public Result<String> updateLastMsg_V2(@RequestParam Long convId, @RequestParam Long lastMsgId_new){
        iConversationUserService.updateLastMsg_V2(convId, lastMsgId_new);
        return Result.success();
    }


    /** 查（查询用户在当前会话中上次阅读到的位置，返回上次阅读的消息id）
     * @param convId 会话ID
     * @return 上次阅读的消息id
     */
    @GetMapping("/queryLastMsg")
    public Result<Long> queryLastMsg(@RequestParam Long convId){
        Long lastMsgId = iConversationUserService.queryLastMsg(convId);
        return Result.success(lastMsgId);
    }

}
